<script >
import { useBaseClassListApi } from '@/api/baseClass'
import { useTableApi, useTableSubmitApi } from '@/api/table'
import { useDownloadApi, useGeneratorApi } from '@/api/generator'

export default {
  name: 'Generator',
  data() {
    return {
      visible: false,
      baseClassList: [],
      dataForm: {
        id: '',
        baseclassId: '',
        generatorType: 0,
        formLayout: 1,
        backendPath: '',
        frontendPath: '',
        packageName: '',
        email: '',
        author: '',
        version: '',
        moduleName: '',
        functionName: '',
        className: '',
        tableComment: '',
        tableName: ''
      },
      dataRules: {
        tableName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
        tableComment: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
        className: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
        packageName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
        author: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
        moduleName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
        functionName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
        generatorType: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
        formLayout: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
        backendPath: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
        frontendPath: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
      },
      refInstance: null
    }
  },
  mounted() {
    this.refInstance = this.$refs.dataFormRef
  },
  methods: {
    getBaseClassList() {
      useBaseClassListApi().then(res => {
        this.baseClassList = res.data
      })
    },
    getTable(id) {
      useTableApi(id).then(res => {
        Object.assign(this.dataForm, res.data)
      })
    },
    init(id) {
      this.visible = true
      this.dataForm.id = ''
      this.getBaseClassList()
      this.getTable(id)
    },
    submitHandle() {
      this.$refs.dataFormRef.validate((valid) => {
        if (valid) {
          useTableSubmitApi(this.dataForm).then(() => {
            this.$message.success({
              message: '操作成功',
              duration: 500,
              onClose: () => {
                this.visible.value = false
                this.$emit('refreshDataList')
              }
            })
          })
          this.visible = false
          this.$emit('submit', this.dataForm)
        } else {
          return false
        }
      })
    },
    generatorHandle() {
      this.$refs.dataFormRef.validate(async(valid) => {
        if (!valid) {
          return false
        }

        // 先保存
        await useTableSubmitApi(this.dataForm)

        // 生成代码，zip压缩包
        if (this.dataForm.generatorType === 0) {
          useDownloadApi([this.dataForm.id])
          this.visible = false
          return
        }

        // 生成代码，自定义路径
        useGeneratorApi([this.dataForm.id]).then(() => {
          this.$message.success({
            message: '操作成功',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        })
      })
    }
  }
}
</script>

<template>
  <el-dialog :visible.sync="visible" title="生成代码" :close-on-click-modal="false">
    <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="120px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="表名" prop="tableName">
            <el-input v-model="dataForm.tableName" disabled placeholder="表名" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="说明" prop="tableComment">
            <el-input v-model="dataForm.tableComment" placeholder="说明" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="类名" prop="className">
            <el-input v-model="dataForm.className" placeholder="类名" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="baseclassId" label="继承">
            <el-select v-model="dataForm.baseclassId" placeholder="继承" style="width: 100%" clearable>
              <el-option v-for="item in baseClassList" :key="item.id" :label="item.code" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="模块名" prop="moduleName">
            <el-input v-model="dataForm.moduleName" placeholder="模块名" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="功能名" prop="functionName">
            <el-input v-model="dataForm.functionName" placeholder="功能名" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目包名" prop="packageName">
            <el-input v-model="dataForm.packageName" placeholder="项目包名" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="版本号" prop="version">
            <el-input v-model="dataForm.version" placeholder="版本号" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="默认作者" prop="author">
            <el-input v-model="dataForm.author" placeholder="默认作者" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="作者邮箱" prop="email">
            <el-input v-model="dataForm.email" placeholder="作者邮箱" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="生成方式" prop="generatorType">
            <el-radio-group v-model="dataForm.generatorType">
              <el-radio :label="0">zip压缩包</el-radio>
              <el-radio :label="1">自定义路径</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="表单布局" prop="formLayout">
            <el-radio-group v-model="dataForm.formLayout">
              <el-radio :label="1">一列</el-radio>
              <el-radio :label="2">两列</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item v-if="dataForm.generatorType === 1" label="后端生成路径" prop="backendPath">
        <el-input v-model="dataForm.backendPath" placeholder="后端生成路径" />
      </el-form-item>
      <el-form-item v-if="dataForm.generatorType === 1" label="前端生成路径" prop="frontendPath">
        <el-input v-model="dataForm.frontendPath" placeholder="前端生成路径" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submitHandle()">保存</el-button>
      <el-button type="danger" @click="generatorHandle()">生成代码</el-button>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss">

</style>
